package com.yenlien.traceyun.control.controller;


import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.entity.Message;
import com.yenlien.traceyun.control.interceptor.UserAuthUtil;
import com.yenlien.traceyun.control.service.MessageService;
import com.yenlien.traceyun.control.service.rest.RestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;


/**
 * Created by LiuSha on 2020/6/10.
 */
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {
    @Autowired
    private RestService restService;

    @Autowired
    private MessageService messageService;

    /**
     * 针对用户的一个列表查询（分状态处理）
     */
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody Message message) {
        message.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(messageService.findByList(message));       //列表分页查询
    }


    /**
     * 数据中心保存数据，进入待发送状态
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody JSONObject params) {
        return success(messageService.save(params));
    }

    /**
     * 针对用户的一个读取消息的一个状态更改
     */
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public Object update(@PathVariable("id") String id) {
        return success(messageService.updateStatus(id));
    }

    /**
     * 数据中心的编辑功能
     */
    @RequestMapping(value = "/updateAll", method = RequestMethod.GET)
    public Object updateAll(@RequestBody JSONObject params) {
        return success(messageService.updateAll(params));
    }

    /**
     * 用户端查询单条数据，根据id查询
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable String id) {
        return success(messageService.findById(id));
    }

    /**
     * 数据中心的编辑功能   首先根据pid进行数据查询找出修改的这条数据
     */
    @RequestMapping(value = "/findByPid/{pid}", method = RequestMethod.GET)
    public Object findByPid(@PathVariable("pid") String pid) {
        return success(messageService.findByPid(pid));
    }


    /**
     * 发送消息数据中心----整个列表查询
     */
    @RequestMapping(value = "/findByListSend", method = RequestMethod.POST)     //列表群发统计
    public Object findByListSend(@RequestBody Message message) {
        message.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        Page<Message> page = messageService.findByListSend(message);
        List<Message> contentGroup = page.getContent();
        List<Message> contentAll = messageService.findByAll();
        List<Message> li = new ArrayList<>();
        for (Message messageGroup : contentGroup) {
            StringJoiner sj = new StringJoiner(",");
            String pid = messageGroup.getPid();
            for (Message messageAll : contentAll) {
                String pidsun = messageAll.getPid();
                if (pid.equals(pidsun)) {
                    sj.add(messageAll.getCompanyName());
                }
            }
            messageGroup.setCompanyName(sj.toString());
            li.add(messageGroup);
        }
        Page<Message> pageTotal = new PageImpl<Message>(li, page.getPageable(), page.getTotalElements());
        return buildPage(pageTotal);       //列表分页查询
    }


    /**
     * 数据中心根据pid进行消息删除   （删除的是整个pid下面的消息）
     */
    @RequestMapping(value = "/deleteByPid/{pid}", method = RequestMethod.GET)
    public Object deleteByPid(@PathVariable String pid) {
        return success(messageService.deleteByPid(pid));
    }

    /**
     * 数据中心群发消息
     */
    @RequestMapping(value = "/sendAllStatus/{pid}", method = RequestMethod.GET)
    public Object sendAllStatus(@PathVariable("pid") String pid) {
        return success(messageService.updateAllStatus(pid));
    }

}
